﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.cms.businesslogic.datatype;
using uTube.Core.DataTypes.PrevalueEditors;
using uTube.Core.Extensions;
using uTube.Core.Properties;

namespace uTube.Core.DataTypes.YouTubeUploader
{
    public class YTU_PrevalueEditor : AbstractJsonPrevalueEditor
    {
        /// <summary>
        /// TextBox control for the application name.
        /// </summary>
        private TextBox txtApplicationName;

        /// <summary>
        /// TextBox control for the developer key.
        /// </summary>
        private TextBox txtDeveloperKey;

        /// <summary>
        /// TextBox control for the clientlogin username.
        /// </summary>
        private TextBox txtUsername;

        /// <summary>
        /// TextBox control for the clientlogin password.
        /// </summary>
        private TextBox txtPassword;

        /// <summary>
        /// Initializes a new instance of the <see cref="YTU_PrevalueEditor"/> class.
        /// </summary>
        /// <param name="dataType">Type of the data.</param>
        public YTU_PrevalueEditor(BaseDataType dataType)
            : base(dataType, DBTypes.Ntext)
        {
        }

        /// <summary>
        /// Saves the data-type PreValue options.
        /// </summary>
        public override void Save()
        {
            string applicationName = this.txtApplicationName.Text;
            if (string.IsNullOrEmpty(applicationName))
            {
                applicationName = Settings.Default.ApplicationName;
            }

            string developerKey = this.txtDeveloperKey.Text;
            if (string.IsNullOrEmpty(developerKey))
            {
                developerKey = Settings.Default.DeveloperKey;
            }

            // set the options
            var options = new YTU_Options()
            {
                ApplicationName = applicationName,
                DeveloperKey = developerKey,
                Username = this.txtUsername.Text,
                Password = this.txtPassword.Text
            };

            // save the options as JSON
            this.SaveAsJson(options);
        }

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.EnsureChildControls();
        }

        /// <summary>
        /// Creates child controls for this control
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // set-up child controls
            this.txtApplicationName = new TextBox() { ID = "txtApplicationName", CssClass = "guiInputText umbEditorTextField" };
            this.txtDeveloperKey = new TextBox() { ID = "txtDeveloperKey", CssClass = "guiInputText umbEditorTextField" };
            this.txtUsername = new TextBox() { ID = "txtUsername", CssClass = "guiInputText umbEditorTextField" };
            this.txtPassword = new TextBox() { ID = "txtPassword", CssClass = "guiInputText umbEditorTextField", TextMode = TextBoxMode.Password };

            // add the child controls
            this.Controls.AddPrevalueControls(this.txtApplicationName, this.txtDeveloperKey, this.txtUsername, this.txtPassword);
        }

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Load"/> event.
        /// </summary>
        /// <param name="e">The <see cref="T:System.EventArgs"/> object that contains the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // get PreValues, load them into the controls.
            var options = this.GetPreValueOptions<YTU_Options>();

            // if the options are null, then load the defaults
            if (options == null)
            {
                options = new YTU_Options(true);
            }

            // set the values
            this.txtApplicationName.Text = options.ApplicationName;
            this.txtDeveloperKey.Text = options.DeveloperKey;
            this.txtUsername.Text = options.Username;
            this.txtPassword.Text = options.Password;
        }

        /// <summary>
        /// Renders the contents of the control to the specified writer. This method is used primarily by control developers.
        /// </summary>
        /// <param name="writer">A <see cref="T:System.Web.UI.HtmlTextWriter"/> that represents the output stream to render HTML content on the client.</param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
            // add property fields
            writer.AddPrevalueHeading("YouTube API Settings");
            writer.AddPrevalueRow("", "If you wish to use your own YouTube application name and developer key, you can do so here.");
            writer.AddPrevalueRow("Application Name:", this.txtApplicationName);
            writer.AddPrevalueRow("Developer Key:", this.txtDeveloperKey);

            writer.AddPrevalueHeading("YouTube Account Settings");
            ////writer.AddPrevalueRow("", "If you wish to use ClientLogin authentication when uploading your videos to YouTube, please enter your YouTube Username and Password below.");
            writer.AddPrevalueRow("", "Please enter your YouTube Username and Password below (required for uploading videos)");
            writer.AddPrevalueRow("Username:", this.txtUsername);
            writer.AddPrevalueRow("Password:", this.txtPassword);
        }
    }
}